<?php
/**
 * 客户后台对产品类别隐藏和显示顺序的管理
 *
 * Author: lyun
 * Date: 2018/5/16
 * Created by Panxsoft.
 */
namespace App\Http\Controllers\Admin\Customer\Product;

use App\Http\Controllers\ApiController;
use App\Http\Requests\Admin\Customer\Product\CategoryIndexRequest;
use App\Http\Requests\Admin\Customer\Product\CategoryUpdateRequest;
use App\Models\Admin\CustomerAdmin;
use App\Models\Customer\Language;
use App\Models\Panx\Category;
use App\Models\Panx\CategoryPivotCustomer;
use App\Services\Utils\Guard;
use Illuminate\Database\Eloquent\Collection;
use Illuminate\Pagination\LengthAwarePaginator;

class CategoryController extends ApiController
{
    const ROUTES = [
        'index'  => 'api.admin.customer.category.index',
        'update' => 'api.admin.customer.category.update',
    ];

    /**
     * Display a listing of the resource.
     * @test
     *
     * @param CategoryIndexRequest $request
     * @return \Illuminate\Http\Response
     * @throws \Exception
     */
    public function index(CategoryIndexRequest $request)
    {
        $this->checkIsTrue(CustomerAdmin::isGuardSuperAdmin(), trans('admin.is_super_man'));

        /** @var CustomerAdmin $customer_admin */
        $customer_admin = Guard::customerAdmin();
        $customer = $customer_admin->customer;
        $language_id = Language::mapCodeToId($customer_admin->language);

        /** @var Collection|LengthAwarePaginator $categories */
        $categories = $customer->categories()
            ->filterCustomerPivotIsShow($request->is_show)
            ->with('categoryDescriptions')
            ->orderBy((new CategoryPivotCustomer)->qualifyColumn('priority'), 'desc')
            ->paginate($request->per_page ?: 15);

        $categories->transform(function(Category $category) use($language_id){
            $category_descriptions = $category->categoryDescriptions->pluck('name', 'language_id');
            $category->name = $category_descriptions[$language_id] ?? null;
            $category->const = $category_descriptions[Language::LANGUAGE_ID_EN] ?? null;

            return $category;
        });

        return $this->response(compact('categories'));
    }

    /**
     * Update the specified resource in storage.
     * @test
     *
     * @param  CategoryUpdateRequest  $request
     * @param  int  $id
     * @return \Illuminate\Http\Response
     * @throws \Exception
     */
    public function update(CategoryUpdateRequest $request, $id)
    {
        $this->checkIsTrue(CustomerAdmin::isGuardSuperAdmin(), trans('admin.is_super_man'));

        /** @var CategoryPivotCustomer $category_pivot_customer */
        $category_pivot_customer = (new CategoryPivotCustomer)
            ->where('customer_id', Guard::customerAdmin()->customer_id)
            ->find($id);
        $this->checkItemExist($category_pivot_customer, trans('admin.category_pivot_customer'));

        $category_data = $this->assembleCategoryData($request);

        $updated = $category_pivot_customer->update($category_data);

        return $this->response(compact('updated'));
    }

    /**
     * @param CategoryUpdateRequest $request
     * @return array
     */
    private function assembleCategoryData(CategoryUpdateRequest $request)
    {
        return [
            'priority' => $request->priority,
            'is_show'  => $request->is_show,
        ];
    }
}
